package com.ku.log.service;

import com.ku.log.dao.LogMapper;
import com.ku.log.dao.factory.LogMapperFactory;
import com.ku.log.entity.SysLog;
import com.ku.log.support.constant.ResultConstant;
import com.ku.log.util.ThreadLocals;
import com.ku.log.vo.LogQueryVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;

/**
 * @author: GZQ
 * @create: 2020/08/09
 * @description: 封装日志操作
 **/
@Component("logService")
@Slf4j
public class LogService {

    @Value("${spring.datasource.driver-class-name}")
    private String jdbcDriver;
    @Autowired
    private LogMapperFactory factory;
    /**
     * 单例LogMapper
     */
    private volatile LogMapper logMapper = null;

    private synchronized LogMapper getLogMapper() {
        if (logMapper == null) {
            logMapper = factory.getLogMapper(jdbcDriver);
        }
        return logMapper;
    }

    /**
     * 添加日志
     * @param sysLog 操作日志实体
     */
    public void addLog(SysLog sysLog){
        if (sysLog!=null){
            getLogMapper().insert(sysLog);
        }else {
            log.info("本次操作未生成日志数据...");
        }
    }

    /**
     * 查询日志
     * @param logQueryVo 日志查询VO类
     * @return
     */
    public List<LogQueryVo> queryLog(LogQueryVo logQueryVo) {
        return getLogMapper().findBySelective(logQueryVo);
    }

    /**
     * 设置当前线程日志参数
     * @param desc 为本地线程设置参数
     */
    public void setLogDesc(String desc) {
        // 做try-catch，避免影响业务
        try {
            SysLog localLog = ThreadLocals.getLocal(SysLog.class);
            if (localLog != null) {
                localLog.setOperateDesc(desc == null ? "" : desc);
            }
        } catch (Exception e) {
            log.info("操作日志-修改操作目标参数时异常: " + e.getMessage());
        }
    }
    public void descAppend(String desc) {
        try {
            // 做try-catch，避免影响业务
            SysLog localLog = ThreadLocals.getLocal(SysLog.class);
            if (localLog != null) {
                String operateDesc = localLog.getOperateDesc();
                localLog.setOperateDesc(operateDesc == null ? desc : operateDesc + desc);
            }
        } catch (Exception e) {
            log.info("操作日志-修改操作目标参数时异常: " + e.getMessage());
        }
    }

    public void failed() {
        try {
            SysLog localLog = ThreadLocals.getLocal(SysLog.class);
            if (localLog != null) {
                localLog.setResult(ResultConstant.FAILED);
            }
        } catch (Exception e) {
            log.info("操作日志-设置操作失败时异常: " + e.getMessage());
        }
    }

    public void updateType(String type) {
        try {
            SysLog localLog = ThreadLocals.getLocal(SysLog.class);
            if (localLog != null) {
                localLog.setOperateType(type);
            }
        } catch (Exception e) {
            log.info("操作日志-设置操作类型时异常: " + e.getMessage());
        }
    }

    public void updateItem(String item) {
        try {
            SysLog localLog = ThreadLocals.getLocal(SysLog.class);
            if (localLog != null) {
                localLog.setItem(item);
            }
        } catch (Exception e) {
            log.info("操作日志-更新操作项时异常: " + e.getMessage());
        }
    }

    public int countQueryLog(LogQueryVo logQueryVo) {
        return getLogMapper().countBySelective(logQueryVo);
    }

    public void updateModule(String module) {
        try {
            SysLog localLog = ThreadLocals.getLocal(SysLog.class);
            if (localLog != null) {
                localLog.setModule(module);
            }
        } catch (Exception e) {
            log.info("操作日志-更新模块名称时异常: " + e.getMessage());
        }
    }
}
